package com.xinli.business.controller;

import java.math.BigDecimal;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ijpay.core.IJPayHttpResponse;
import com.ijpay.core.enums.RequestMethodEnum;
import com.ijpay.core.kit.PayKit;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.enums.WxDomainEnum;
import com.ijpay.wxpay.enums.v3.BasePayApiEnum;
import com.ijpay.wxpay.model.v3.RefundAmount;
import com.ijpay.wxpay.model.v3.RefundGoodsDetail;
import com.ijpay.wxpay.model.v3.RefundModel;
import com.xinli.business.domain.TOrder;
import com.xinli.business.domain.TOrderRefund;
import com.xinli.business.service.ITOrderRefundService;
import com.xinli.business.service.ITOrderService;
import com.xinli.common.utils.DateUtils;
import com.xinli.pay.wxpay.config.WxPayV3Bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xinli.common.annotation.Log;
import com.xinli.common.core.controller.BaseController;
import com.xinli.common.core.domain.AjaxResult;
import com.xinli.common.enums.BusinessType;;
import com.xinli.common.utils.poi.ExcelUtil;
import com.xinli.common.core.page.TableDataInfo;

/**
 * 订单售后原因Controller
 *
 * @author ruoyi
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/business/refund")
public class TOrderRefundController extends BaseController
{
    @Autowired
    private ITOrderRefundService tOrderRefundService;

    @Autowired
    private ITOrderService orderService;


    /**
     * 查询订单售后原因列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TOrderRefund tOrderRefund)
    {
        startPage();
        List<TOrderRefund> list = tOrderRefundService.selectTOrderRefundList(tOrderRefund);
        return getDataTable(list);
    }

    /**
     * 导出订单售后原因列表
     */
    @Log(title = "订单售后原因", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOrderRefund tOrderRefund)
    {
        List<TOrderRefund> list = tOrderRefundService.selectTOrderRefundList(tOrderRefund);
        ExcelUtil<TOrderRefund> util = new ExcelUtil<TOrderRefund>(TOrderRefund.class);
        util.exportExcel(response, list, "订单售后原因数据");
    }

    /**
     * 获取订单售后原因详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tOrderRefundService.selectTOrderRefundById(id));
    }

    /**
     * 新增订单售后原因
     */
    @Log(title = "订单售后原因", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOrderRefund tOrderRefund)
    {
        return toAjax(tOrderRefundService.save(tOrderRefund));
    }

    /**
     * 修改订单售后原因
     */
    @Log(title = "订单售后原因", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOrderRefund vo)
    {
        TOrderRefund refund = tOrderRefundService.getById(vo.getId());
        refund.setCheckStatus(vo.getCheckStatus());
        refund.setCheckRemark(vo.getCheckRemark());
        // 处理时间
        refund.setHandelTime(DateUtils.getNowDate());
        if (vo.getCheckStatus().equals("2")){
            TOrder order = orderService.getById(refund.getOrderId());
            //申诉成功
            order.setRefundStatus("1");
            orderService.updateById(order);
        }else{
            TOrder order = orderService.getById(refund.getOrderId());
            //申诉失败
            order.setRefundStatus("2");
            orderService.updateById(order);
        }
        tOrderRefundService.updateById(refund);
        return AjaxResult.success();
    }



    /**
     * 删除订单售后原因
     */
    @Log(title = "订单售后原因", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tOrderRefundService.removeByIds(Arrays.asList(ids)));
    }
}
